/**
 * 专题配置
 * shilei
 */
var themeConfig = (function(){
	/*********数据专题***********************************/
	//数据专题面板
	var dataThemePanelInit=function(){
		$('#dataTheme').panel({  
			  width:260,  
			  height:500,
			  title: '数据专题',
			  tools: [{
			    iconCls:'icon-add',  
			    handler:function(){//新增节点
			    	//先获取当前选中的节点
			    	debugger
			    	var selected = $('#layout_center_dataThemeTree').tree('getSelected');
			    	if(selected != null){
			    		$('#themeName').val("");
			    		$('#dataThemeTreeAddPopWin').css("display","block");
			    		$('#dataThemeTreeAddPopWin').window({
				    	    width:345,
				    	    height:80,
				    	    modal:true,
				    	    title:'添加数据',
				    	    minimizable:false,
				    	    maximizable:false,
				    	    closable:true,
				    	    collapsible:false,
				    	    constrain:true,
				    	    left:200,
				    	    top:80,
				    	    
				    	});
			    		$("form#addDataTheme #okBtn").unbind('click').click(function(){
			    		   var parentNodeId = $('#layout_center_dataThemeTree').tree('getSelected').id;
			    		   $('form#addDataTheme input[name=parentNodeId]').val(parentNodeId);
				    		debugger
				    		$('form#addDataTheme').submit();
				    	});
			    	}else{
			    		$.messager.show({
				        	title:'提示',
				        	msg:"请选择一个数据专题"
				        });
			    	}
			    }  
			  },{  
			    iconCls:'icon-del',  
			    handler:function(){//删除节点
			    	var selected = $('#layout_center_dataThemeTree').tree('getSelected');
			    	if(selected != null){
			    		$.post(basePath+'/DataThemeController/delTreeNode',{"id":selected.id},function(response){
			    			debugger
							 var obj=jQuery.parseJSON(response);
						        if(obj.success){
							        //刷新一下树
							        $('#layout_center_dataThemeTree').tree('reload');
						        }
						        $.messager.show({
						        	title:'提示',
						        	msg:obj.msg
						        });
			    		}.bind(this));
			    	}else{
			    		$.messager.show({
				        	title:'提示',
				        	msg:"请选择一个数据专题"
				        });
			    	}
			    }  
			  }]
		});
		$('#dataTheme').closest(".panel").css({"padding":"0px 10px","display":"inline-block","margin-top":"10px","margin-left":"10px"});
	};
	//数据专题树数据
	var dataThemeTreeInit=function(){
		debugger
		$("#layout_center_dataThemeTree").tree({
		    url:basePath+'/DataThemeController/tree',
			lines:true,//默认是false
  			checkbox:false,//默认是false   
  			onlyLeafCheck:false,//默认是false
  			cascadeCheck:true,//默认是true
  			parentField:'pid',
  			dnd:false,//默认是false, 是否能够拖拽 ； 设置为true的时候,该节点就是可以拖拽的，可以移动位置
  			onDblClick: function(node){
  				debugger
				$(this).tree('beginEdit',node.target);
			},
			onAfterEdit:function(node){
				debugger
				$.post(basePath+'/DataThemeController/editTreeNode',{"id":node.id,"text":node.text},
					function(response){
						var obj=jQuery.parseJSON(response);
				        if(obj.success){
					        //刷新一下树
					        $('#layout_center_dataThemeTree').tree('reload');
				        }
				        $.messager.show({
				        	title:'提示',
				        	msg:obj.msg
				        });
				});
			},
			onClick: function(node){
  				debugger
  				var isLeaf =  $('#layout_center_dataThemeTree').tree('isLeaf',node.target);
  				if(isLeaf){
  					$.post(basePath+'/DataThemeController/getDefinedExpress',{"id":node.id},function(response){
  						debugger
  						var obj=jQuery.parseJSON(response);
  						$.messager.show({
				        	title:'提示',
				        	msg:obj.msg
				        });
				        if(obj.success){
				        	if(obj.msg == "找到关联信息"){
				        		var layerNodeID = obj.result.layerId;
					        	//将该节点选中
					        	var node = $("#layout_center_serviceResourceTree").tree('find',layerNodeID);
					        	$("#layout_center_serviceResourceTree").tree('select',node.target);
					        	//发送请求获取所有的字段信息
					        	var fields=obj.result.fields;
					        	getSelLayerAllFields(node);
	  						    $("#expressPanel #expressContent").text(fields);
	  						    $("#editTablePanel #layercanedit")[0].checked = obj.result.edittable==1?true:false;
	  						    $("#editTablePanel #TiledLayer")[0].checked = obj.result.tileFlag==1?true:false;
	  						    $("#showLevelPanel #showlevelset")[0].selectedIndex = obj.result.changelevel-9;
				        	}else{
				        		//取消图层树节点
				        		var node = $("#layout_center_serviceResourceTree").tree('getSelected');
				        		if(node){
				        			 $('#layout_center_serviceResourceTree').find('.tree-node-selected').removeClass('tree-node-selected');
				        		}
				        		//清除字段面板
				        		$("#conditionConfig #fieldPanel").empty();
				        		//清除字段值面板
				        		$("#conditionConfig #valuePanel").empty();
				        		//清除表达式面板
				        		$("#expressPanel #expressContent").text('');
				        	}
				        }
  					});
  				}
			},
		});
	};
	/****服务资源*********************************************/
	//服务资源面板
	var serviveSourcePanelInit=function(){
		//服务资源panel
		$('#serviceResource').panel({  
			  width:260,  
			  height:500,
			  title:'服务资源'
		});
		//嵌套字段panel
		$("#fieldPanel").panel({
			 width:130,  
			 height:350,
			 title:'字段'
		});
		//嵌套值panel
		$("#valuePanel").panel({
			 width:130,  
			 height:350,
			 title:'唯一值'
		});
		//嵌套表达式panel
		$("#expressPanel").panel({
			 title:'表达式',
			 //height:'100%'
		});
		//要素表是否可编辑
//		$("#editTablePanel").panel({
//			 title:'图层可否编辑',
//			 height:'100%'
//		});
		debugger
		$('#serviceResource').closest(".panel").css({"display":"inline-block","margin-top":"10px"});
		$('#fieldPanel').panel("options").width="100%";
		$('#valuePanel').panel("options").width="100%";
		$('#fieldPanel').prev().css({"border-right-style":"hidden","border-left-style":"hidden","text-align":"center"});
		$('#fieldPanel').css({"border-right-style":"hidden","border-left-style":"hidden"});
		$('#valuePanel').prev().css({"border-right-style":"hidden","text-align":"center"});
		$('#valuePanel').css({"border-right-style":"hidden"});
		$('#expressPanel').prev().css({"border-top-style":"hidden","border-left-style":"hidden","border-right-style":"hidden"});
		$('#expressPanel').css({"border-style":"hidden"});
	};
	//服务资源树数据
	var serviceResourceTreeInit=function(){
		$("#layout_center_serviceResourceTree").tree({
			url:basePath+'/ServerResourceController/tree',
			lines:true,//默认是false
  			checkbox:false,//默认是false
  			onlyLeafCheck:false,//默认是false
  			cascadeCheck:true,//默认是true
  			parentField:'pid',
  			dnd:false,//默认是false, 是否能够拖拽 ； 设置为true的时候,该节点就是可以拖拽的，可以移动位置
  			onClick:function(node){
  				debugger
  				var selected = $('#layout_center_dataThemeTree').tree('getSelected');
  				if(selected != null){
  					var isLeaf =  $('#layout_center_dataThemeTree').tree('isLeaf',selected.target);
  					if(!isLeaf){
  						$.messager.show({title:'提示',msg:"请在数据专题中选择一个叶子节点"});
  					}else{
  						isLeaf = $(this).tree('isLeaf',node.target);
  						if(!isLeaf){
  							$.messager.show({title:'提示',msg:"请在服务资源中选择一个叶子节点"});
  						}else{
  							//此时需要在条件配置页面中添加内容
  							debugger
  							getSelLayerAllFields(node);
  						}
  					}
  				}else{
  					 $.messager.show({title:'提示',msg:"请在数据专题中选择一个叶子节点"});
  				}
			}
		});
	};
	//表单初始化
	var addDataThemeFormInit = function(){
		$('form#addDataTheme').form({url:basePath+'/DataThemeController/addTreeNode',
			success:function(r){
				 debugger
				 var obj=jQuery.parseJSON(r);
			        if(obj.success){
			        	$('#dataThemeTreeAddPopWin').window('close');
			        	//刷新一下树
				        $('#layout_center_dataThemeTree').tree('reload');
			        }
			        $.messager.show({
			        	title:'提示',
			        	msg:obj.msg
			        });
			}});
	};
	var getSelLayerAllFields=function(node){
		$.post(basePath+'/DataThemeController/getConditionConfig',{"id":node.id,"pid":node.pid},function(response){
			debugger
			var obj=jQuery.parseJSON(response);
			var fields="";
			var html="";
		    if(obj.success){
		    	$("#conditionConfig #fieldPanel").empty();
		    	fields=jQuery.parseJSON(obj.result).fields;
		    	$.each(fields,function(i,item){
		    		html+='<div style="display:list-item;padding:5px;">'+item["Name"]+'</div>';
		    	});
		    	$("#conditionConfig #fieldPanel").append(html);
		    	debugger
		    	conditionConfigElmEvtBind();
		    }
			});
	};
	
	/****条件配置*********************************************/
	window.currentFiledClick=null;
	window.currentFiledValueClick=null;
	var conditionConfigPanelInit=function(){
		$('#conditionConfig').panel({  
			  width:300,  
			  height:500,
			  title:'条件配置'
		});
		$('#conditionConfig').closest(".panel").css({"display":"inline-block","margin-top":"10px"});
	};
	
	//为条件配置页面中动态添加的元素绑定事件
	var conditionConfigElmEvtBind=function(){
		var timer = null;
		//字段面板绑定单击事件
        $("div#fieldPanel > div").bind("click", function(){//单击事件  
            clearTimeout(timer);  
            timer = setTimeout(getFieldValues(this), 300);
        }); 
        //字段面板绑定双击事件
        $("div#fieldPanel > div").bind("dblclick", function(){//双击事件 
        	debugger
            clearTimeout(timer);//在双击事件中，先清除前面click事件的时间处理
        	addContextToExpress(this);
        });
        //运算符绑定单击事件
        $("div#expressPanel a").unbind('click').click(function(){
        	addContextToExpress(this,"field");
        });
	};
	var fieldValueBindEvtClick = function(){
		 $("div#valuePanel > div").unbind('click').bind("click",function(){//单击事件
			 addContextToExpress(this,"value");
	     });
	};
	//添加内容到表达式面板中
	var addContextToExpress=function(obj,flag){
		debugger
		var expressText=$("#expressPanel #expressContent").text();
		if($(obj).text() == 'AND' || $(obj).text() == 'OR'){
			expressText+=' '+$(obj).text()+' ';
		}else{
			expressText+=(flag=='value')?"'"+$(obj).text()+"'":$(obj).text();
		}
        $("#expressPanel #expressContent").text(expressText);
	}
	//获取属性字段值
	var getFieldValues=function(obj){
		debugger
        var selected = $('#layout_center_serviceResourceTree').tree('getSelected');
       	var field=$(obj).text();
       	$.post(basePath+'/DataThemeController/getFieldValue',{"id":selected.id,"pid":selected.pid,"field":field},function(response){
			debugger;
			var obj=jQuery.parseJSON(response);
			var fieldValues="";
			var html="";
		    if(obj.success){
		    	$("#conditionConfig #valuePanel").empty();
		    	fieldValues=jQuery.parseJSON(obj.result).UniqueValue;
		    	$.each(fieldValues,function(i,item){
		    		html+='<div style="display:list-item;padding:5px;">'+item+'</div>';
		    	});
		    	$("#conditionConfig #valuePanel").append(html);
		    	debugger
		        //为字段值绑定单击事件
		    	fieldValueBindEvtClick();
		    }
		});
	};
	/****按钮组*********************************************/
	var clearExpressContent=function(){
		debugger
		$("#clearExpress").click(function(){
			 $("#expressPanel #expressContent").text("");
		});
	};
	var saveExpressContent=function(){
		$("#saveExpress").click(function(){
			debugger
			////////#####################
			var layerTiled=$("#editTablePanel #TiledLayer")[0].checked;
			//###########################
			var layercanedit = $("#editTablePanel #layercanedit")[0].checked;
			var showFlLevel = $("#showLevelPanel #showlevelset")[0].value;
			var expressContent=$("#expressPanel #expressContent").text().trim();
			var selDataTheme = $('#layout_center_dataThemeTree').tree('getSelected');
			var selLayer = $('#layout_center_serviceResourceTree').tree('getSelected');
			
			/////ANANANANNANA//
			$.post(basePath+'/DataThemeController/saveExpress',{"resourceId":selDataTheme.id,"layerId":selLayer.id,"fields":expressContent,"edittable":(layercanedit?1:0),"showfllevel":showFlLevel,"tileFlag":(layerTiled?1:0)},function(response){
				debugger
				var obj=jQuery.parseJSON(response);
			    if(obj.success){
			    	//$("#expressPanel #expressContent").text("");
			    }
			    $.messager.show({title:'提示',msg:obj.msg});
			});
		    ///////////ANANANNA/////////
		});
	};
	return {
		init:function(){
			//数据专题
			dataThemePanelInit();
			dataThemeTreeInit();
			addDataThemeFormInit();
			
			//服务资源
			serviveSourcePanelInit();
			serviceResourceTreeInit();
			
			//条件配置
			conditionConfigPanelInit();
			
			//初始化按钮绑定事件
			clearExpressContent();
			
			//保存表达式
			saveExpressContent();
		}
	}
})();